/*************************************************************************
    > File Name: main.c
    > Author: ma6174
    > Mail: ma6174@163.com 
    > Created Time: 2022年10月27日 星期四 16时07分53秒
 ************************************************************************/

#include"buffer.h"

extern size_t getFileSize(int fd);
extern void *thread_copy(void *arg);
extern void copyfile(char *src, char *dst);
extern void copy(char *src, char *dst);
extern void Progress();
extern void del_file(char *path);
extern void del_dir(char *path);
extern int comp(const void *a, const void *b);
extern void copysrc_to_dst(char *src, char *dst);
extern void dir_traverse(char *path1, char *path2);
//extern void comAB(char *path1, char *path2);
extern void Sync(char *src,char *dest);
extern void AB_Sync(char *src,char *dest);

extern int finddir(char *path);
extern int find_file(char *path,char *filename);
extern int CompareFile(char *file1, char *file2);
extern void dir_traverse1(char *src,char *dest);
extern void dir_traverse2(char *src,char *dest);
extern void Sync(char *src,char *dest);

extern void dir_same1(char *src,char *dest);
extern void dir_same2(char *src,char *dest);
extern void AB_Sync(char *src,char *dest);
extern int LastTime(char * file1,char *file2);
extern int change(char *t,int x,int length);


int main(int argc, char *argv[])
{
	char src[100];
	char dest[100];
	DIR *dir1,*dir2;
	int y=1;
	int op;
	printf("\t\t*******************************************\n");
	printf("\t\t\t请进行选择(输入选择的数字)：\n");
	printf("\t\t\t1、备份操作目录AB \n");
	printf("\t\t\t2、同步操作目录AB \n");
	printf("\t\t\t3、退出系统\n");
	printf("\t\t\t输入想选择的数字: ");
	scanf("%d", &op);
	while(op!=3){
		printf("\t\t\t请输入路径A：");
		scanf("%s",src);
		dir1 = opendir(src);
		if(dir1 != NULL) break;
		else{
			perror("src:");
			printf("\t\t\t请重新输入！\n");
		}
	}
	//输入目标路径
	while(op!=3){
		printf("\t\t\t请输入路径B：");
		scanf("%s",dest);
		dir2 = opendir(dest);
		if(dir2 != NULL) break;
		else{
			perror("dest:");
			printf("\t\t\t请重新输入！\n");
		}
	}
	if (op == 1) {
		printf("\t\t\t选项：\n");
		printf("\t\t\t1.目录A备份到目录B\n");
		printf("\t\t\t2.目录B备份到目录A\n");
		printf("\t\t\t3.返回主菜单\n");
		printf("\t\t\t请输入选项：");
		int x;
		scanf("%d",&x);
		if(x<1||x>3)
		printf("\t\t\t输入错误！\n");
		switch(x){
		case 1: copysrc_to_dst(src, dest);break;
		case 2: dir_traverse(src,dest);break;
		case 3: y=0;break;
		}
		
	} else if (op == 2) {
		int i;
		y=0;
		printf("\t\t\t选项：\n");
		printf("\t\t\t1. 目录B同步到目录A\n");
		printf("\t\t\t2. 目录A同步到目录B\n");
		printf("\t\t\t3. AB同时同步，取并集\n");
		printf("\t\t\t4. 返回主菜单\n");
    		printf("\t\t\t请输入选项(1~3):");
    		scanf("%d",&i);
    		if(i<1||i>3){
    			printf("\t\t\t错误，请重新输入！\n");
		}
	switch(i){
		case 1: Sync(src,dest);break;
		case 2: Sync(dest,src);break;
		case 3: AB_Sync(src,dest);break;
		case 4: break;
	}

	}else if(op ==3){
		exit(0);
	}else{
		printf("\t\t\t输入有误! \n");
	}
	if(y==1)
		Progress();
	printf("over \n");

	return 0;
}

